# encoding=utf-8

from tkinter import *
from tkinter import messagebox
import webbrowser
import random


class Application(Frame):
    '''一个经典的GUI程序的类的写法'''

    def __init__(self, master=None):
        super().__init__(master)         # super()代表的是父类的定义，而不是父类对象
        self.master = master
        self.pack()

        self.createWidget()

    def createWidget(self):
        ''' 创建组件 '''
        self.btn01 = Button(self)
        self.btn01["text"] = "click to send flower"
        self.btn01.pack()
        self.btn01["command"] = self.sendFlower

        self.label01 = Label(self,text="百战程序员", width=10, height=2, bg="black", fg="white")
        self.label01.pack()

        # global photo  #把photo声明成全局变量。如果是局部变量，本方法执行完毕后，图像对象销毁，窗口显示不出来
        # photo = PhotoImage(file="imgs/logo.gif")
        # self.label03 = Label(self, image=photo)
        # self.label03.pack()

        self.label04 = Label(self, text="北京尚学堂\n百战程序员\n老高好帅", borderwidth=1,
                             relief="solid", justify="right")
        self.label04.pack()

        #创建一个退出按钮
        self.btnQuit = Button(self, text="退出", anchor=W, command=root.destroy)
        self.btnQuit.pack()

        self.label05 = Label(self, text="用户名")
        self.label05.pack()
        self.label06 = Label(self, text="密码")

        # StringVar变量绑定到指定的组件。
        # StringVar变量的值发生变化，组件内容也变化.
        # 组件内容发生变化，StringVal变量的值也发生变化
        v1 = StringVar()
        self.username = Entry(self, textvariable=v1)
        self.username.pack()
        v1.set("admin")
        print(v1.get())
        print(self.username.get())

        self.label06.pack()
        v2 = StringVar()
        self.pwd = Entry(self, textvariable=v2, show="*")
        self.pwd.pack()

        self.login = Button(self, text="login", command=self.login).pack()
        # self.login.pack()

        self.w1 = Text(root, width=40, height=12, bg="gray")
        self.w1.pack()

        self.w1.insert(1.0, "0123456789\nabcdefg")
        self.w1.insert(2.3, "锄禾日当午，汗滴禾下土。谁知盘中餐，粒粒皆辛苦\n")

        Button(self,text="重复插入文本",command=self.insertText).pack(side="left")
        Button(self, text="返回文本", command=self.returnText).pack(side="left")
        Button(self, text="添加图片", command=self.addImage).pack(side="left")
        Button(self, text="添加组件", command=self.addWidget).pack(side="left")
        Button(self, text="通过tag精确控制文本", command=self.testTag).pack(side="left")

        self.v = StringVar()
        self.v.set("F")

        self.r1 = Radiobutton(root, text="male", value="M", variable=self.v)
        self.r2 = Radiobutton(root,text="female", value="F", variable=self.v)

        self.r1.pack(side = "left")
        self.r2.pack(side="left")

        Button(root, text="confirm", command=self.confirmRadioBtn).pack(side="left")

        self.codeHobby = IntVar()
        self.videoHobby = IntVar()
        print(self.codeHobby.get())
        self.c1 = Checkbutton(self, text="敲代码", variable=self.codeHobby,
                              onvalue=1, offvalue=0)
        self.c2 = Checkbutton(self, text="看视频", variable=self.videoHobby,
                              onvalue=1, offvalue=0)

        self.c1.pack(side="left");self.c2.pack(side="left")

        Button(self, text="确定", command=self.confirm).pack(side="left")

        self.canvas = Canvas(self, width=300, height=200, bg="green")
        self.canvas.pack()
        line = self.canvas.create_line(10,10,30,20,40,50)
        rect = self.canvas.create_rectangle(50,50,100,100)
        oval = self.canvas.create_oval(50,50,100,100)
        global photo
        photo = PhotoImage(file="imgs/logo.gif")
        self.canvas.create_image(150,170,image=photo)

        Button(self, text="画10个矩形", command=self.draw50Recg).pack(side="left")

    def draw50Recg(self):
        for i in range(0,10):
            x1 = random.randrange(int(self.canvas['width'])/2)
            y1 = random.randrange(int(self.canvas['height'])/2)
            x2 = x1 + random.randrange(int(self.canvas['width']) / 2)
            y2 = y1 + random.randrange(int(self.canvas['height']) / 2)
            self.canvas.create_rectangle(x1, y1, x2, y2)

    def confirmRadioBtn(self):
        messagebox.showinfo("Message", self.v.get())

    def confirm(self):
        if self.videoHobby.get() == 1:
            messagebox.showinfo("测试", "看视频，都是正常人有的爱好！你喜欢看什么类型")
        if self.codeHobby.get() ==1:
            messagebox.showinfo("测试", "抓获野生程序员一枚，赶紧送给他尚学堂的视频充饥")

    def insertText(self):
        self.w1.insert(INSERT, "Gaoqi")
        self.w1.insert(END, '[sxt]')

    def returnText(self):
        print(self.w1.get(1.2,1.6))
        print("所有文本内容：\n"+self.w1.get(1.0, END))

    def addImage(self):
        self.photo = PhotoImage(file="imgs/logo.gif")
        self.w1.image_create(END, image=self.photo)

    def addWidget(self):
        b1 = Button(self.w1, text="hello world")
        self.w1.window_create(INSERT, window=b1)

    def testTag(self):
        self.w1.delete(1.0, END)
        self.w1.insert(INSERT, "good good study day day up\n bei jing shang xue tang\n bai zhan cheng xu yuan")
        self.w1.tag_add("good", 1.0, 1.9)
        self.w1.tag_config("good", background="yellow", foreground="red")

        self.w1.tag_add("baidu", 4.0,4.2)
        self.w1.tag_config("baidu", underline=True)
        self.w1.tag_bind("baidu", "<Button-1>", self.webshow)

    def webshow(self,event):
        webbrowser.open("http://www.baidu.com")

    def sendFlower(self):
        messagebox.showinfo("Message", "send flower, 999")

    def login(self):
        username = self.username.get()
        pwd = self.pwd.get()
        print("用户名"+username, "密码"+pwd)

        if username=="gaoqi" and pwd=="123":
            messagebox.showinfo("Message", "success")
        else:
            messagebox.showinfo("Message", "login failed")

if __name__ == '__main__':
    root = Tk()
    root.geometry("500x1450+200+300")
    root.title("一个经典的GUI程序类的测试")
    app = Application(master=root)

    root.mainloop()